Do not promote extern statics - #2302
Open
sjwang05 wants to merge 1 commit into
Open
Conversation
Reference update for rust-lang/rust#157641: a borrow of an extern static is never eligible for constant promotion, even when the reference itself is never read from.
traviscross
approved these changes
Jul 28, 2026
traviscross
reviewed
Jul 28, 2026
Comment on lines
+381
to
+391
| r[destructors.scope.const-promotion.extern-static] | ||
| A borrow of an [`extern` static] cannot be promoted, even if the resulting reference is never read from. | ||
|
|
||
| ```rust,compile_fail | ||
| unsafe extern "C" { | ||
| static X: i32; | ||
| } | ||
| static mut FOO: *const &i32 = [unsafe { &X }].as_ptr(); | ||
| ``` | ||
|
|
Contributor
There was a problem hiding this comment.
If the error produced has an error code, we'll want to add that to the infostring line.
Probably we also want to add some comments to this example to help make clear what's going on. And maybe it's worth adding an admonition that mentions the theory behind why this is rejected (see, e.g., what I wrote in rust-lang/rust#157641 (comment)).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reference update for rust-lang/rust#157641: a borrow of an extern static cannot be promoted, even when the reference is never read from.